package com.dongdongshop.controller;

import com.dongdongshop.shiro.ShiroUtils;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.LoginUserService;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @DubboReference
    private LoginUserService loginUserService;


    @RequestMapping("denglu")
    @ResponseBody
    public AjaxResult denglu(TbUser tbUser){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(tbUser.getUsername(), tbUser.getPassword());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return AjaxResult.R(ResuleEnum.UNKNOWNACCOUNT_ERROR);
        }catch (IncorrectCredentialsException e1){
            return AjaxResult.R(ResuleEnum.INCORRECT_ERROR);
        }
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("getyanzhengma")
    @ResponseBody
    public AjaxResult getCode(String phoneNum){
        //调用Mq 将手机号码传到 MQ当中
        rocketMQTemplate.convertAndSend("dongdongshop-code-topic",phoneNum);
        //alert(短信发送成功......)
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("zhuce1")
    @ResponseBody
    public AjaxResult zhuce1(TbUser tbUser,String massageCode){
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //获取redis中的手机号验证码 没有就发送验证码失败
        if (!redisTemplate.hasKey(tbUser.getPhone())){
                return AjaxResult.R(ResuleEnum.ERROR);
        }
        //获取redis中的手机号验证码
        String phone = (String) valueOperations.get(tbUser.getPhone());
        //如果redis中的验证码和 你输入的验证码一样就可也注册
        if (massageCode.equals(phone)){
            //获取盐
            String salt = ShiroUtils.getSalt(6);
            //获取密文
            String md5Password = ShiroUtils.getMD5Password(salt, tbUser.getPassword());
            tbUser.setPassword(md5Password);
            tbUser.setSalt(salt);
            tbUser.setCreated(new Date());
            tbUser.setUpdated(new Date());
            loginUserService.insertUser(tbUser);
        }
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }
}
